from urllib2 import HTTPError
import web
from gcp import GoogleCloudPrint
from session import need_auth, AuthBase

class ePrintData(AuthBase):
    @need_auth
    def GET(self):
        self.setSessionCookie()
        session = self.getSession()
        try:
            api = GoogleCloudPrint(user=session.user)
            url = web.input().url
            ct, data = api.fetch_data(url)
            web.header('Content-Type', ct)
            web.header('Content-Disposition', 'attachment')
            return data

        except HTTPError:
            raise web.seeother('/signin')
        
